package com.LC.Service.Impl;

import com.LC.DAO.CheckGroupDAO;
import com.LC.Entity.CheckGroup;
import com.LC.Entity.CheckItem;
import com.LC.Entity.PageResult;
import com.LC.Entity.QueryPageBean;
import com.LC.Service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDAO checkGroupDAO;

    @Override
    public List<CheckItem> showAll() {
        return checkGroupDAO.showAll();
    }

    /**
     * 分页方法
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        //使用mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> checkGroupPage = checkGroupDAO.findPage(queryString);
        PageResult pageResult = new PageResult(checkGroupPage.getTotal(), checkGroupPage.getResult());
        return pageResult;
    }

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组数据
        checkGroupDAO.addCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        //新增中间表数据
        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemid : checkitemIds) {
                checkGroupDAO.addMiddleTableCheckGroupAndCheckItem(checkGroupId,checkitemid);
            }
        }else {
            throw new RuntimeException();
        }

    }

    /**
     * 由id查询检查组数据
     * @param CheckGroupId
     * @return
     */
    @Override
    public CheckGroup findCheckGroupById(Integer CheckGroupId) {
        return checkGroupDAO.findCheckGroupById(CheckGroupId);
    }

    /**
     * 由检查组id查询其包含的检查项id
     * @param CheckGroupId
     * @return
     */
    @Override
    public List<Integer> IncludeIds(Integer CheckGroupId) {
        List<Integer> includeIds = checkGroupDAO.IncludeIds(CheckGroupId);
        if (includeIds == null || includeIds.size() == 0){
            throw new RuntimeException();
        }
        return includeIds;
    }

    /**
     * 修改检查组表单和包含的检查项
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void UpdateFormAndIds(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDAO.updateCheckGroup(checkGroup);
        //先删除再新增
        Integer groupId = checkGroup.getId();
        checkGroupDAO.deleteMiddleGroupById(groupId);
        for (Integer checkitemId : checkitemIds) {
            checkGroupDAO.addMiddleTableCheckGroupAndCheckItem(groupId,checkitemId);
        }
    }

    /**
     * 由检查组id删除检查组
     * @param checkGroupId
     */
    @Override
    public void deleteCheckGroupById(int checkGroupId) {
        //删除中间表的数据
        checkGroupDAO.deleteMiddleGroupById(checkGroupId);
        //删除检查组表的数据
        checkGroupDAO.deleteCheckGroupById(checkGroupId);
    }
}
